home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / machack / Hacks97 / AliasSizer.sit / Alias Sizer / source code / INIT.c < prev    next >
C/C++ Source or Header  |  1997-06-28  |  4KB  |  171 lines

  1. //AliasSizer
  2. //MacHack 0x0C
  3. //Copyright ©1997 Barry J. Semo.  All Rights Reserved
  4. //barrys@netcom.com
  5.  
  6. #include "A4Stuff.h"
  7. #include "SetupA4.h"
  8.  
  9. //typedefs
  10. typedef struct SizeResource{
  11.     short flags;
  12.     long preferred;
  13.     long minimum;
  14. }SizeRsrc, *SizeRsrcPtr, **SizeRsrcHandle;
  15.     
  16. typedef pascal short (*HOpenResFileProc)(short vRefNum, long dirID, 
  17.                             ConstStr255Param fileName, SInt8 permission);
  18.  
  19. typedef pascal Handle (*Get1ResourceProc)(ResType theType, short theID);
  20.  
  21.  
  22. //prototypes
  23. pascal short HOpenResFilePatch(short vRefNum, long dirID, 
  24.                             ConstStr255Param fileName, SInt8 permission);
  25.  
  26. pascal Handle Get1ResourcePatch(ResType theType, short theID);
  27. pascal Handle MyGet1ResourceAsm (ResType resType, short resID);
  28.  
  29. //original trap addresses
  30. Get1ResourceProc gOldGet1ResourceAddr;
  31. HOpenResFileProc gOldHOpenResFileAddr;
  32.  
  33. //global variables
  34. SizeRsrc gSizeResource;
  35. Boolean gCheckForSize, gIntercept;
  36.  
  37. void main(void)
  38. {
  39. long oldA4;
  40. Handle h;
  41.  
  42. // a4 world
  43. oldA4 = SetCurrentA4();
  44. RememberA4();
  45.  
  46. // detach init resource
  47. h = Get1Resource('INIT', 0);
  48. if (h) DetachResource(h);
  49.  
  50. //init
  51. gSizeResource.flags = 0;
  52. gSizeResource.preferred = 0L;
  53. gSizeResource.minimum = 0L;
  54. gCheckForSize = false;
  55. gIntercept = false;
  56.  
  57. // patch
  58. gOldHOpenResFileAddr = (HOpenResFileProc)GetToolTrapAddress(_HOpenResFile);
  59. SetToolTrapAddress((UniversalProcPtr)HOpenResFilePatch, _HOpenResFile);
  60.  
  61. gOldGet1ResourceAddr = (Get1ResourceProc)GetToolTrapAddress(_Get1Resource);
  62. SetToolTrapAddress((UniversalProcPtr)MyGet1ResourceAsm, _Get1Resource);
  63.  
  64. //restore
  65. SetA4(oldA4);
  66. }
  67.  
  68. //HOpenResFile patch - catch opening an alias to an application
  69. //then get a SIZE resource out of it, save it for use in a Get1Resource
  70. //call, and set a flag to let the Get1Resource patch know             
  71. extern pascal short HOpenResFilePatch(short vRefNum, long dirID, 
  72.                         ConstStr255Param fileName, SInt8 permission)
  73. {
  74. #pragma unused (permission)
  75.  
  76. long oldA4;
  77. FInfo theInfo;
  78. OSErr err;
  79. short refNum;
  80.  
  81. oldA4 = SetUpA4();
  82.     
  83. //check file type for 'adrp' or, an alias to an application
  84. //other alias types we dont care
  85. err = HGetFInfo(vRefNum, dirID, fileName, &theInfo);
  86. if ((err == noErr) && (theInfo.fdType == 'adrp'))
  87.     {
  88.     gCheckForSize = true;//its the right kind of alias, so set checksize flag
  89.     }
  90.  
  91. //do the real hopenresfile
  92. refNum = gOldHOpenResFileAddr(vRefNum, dirID, fileName, permission);
  93.  
  94. //check to see if flag set for sizechecking and the file opened
  95. // successfully
  96. if (gCheckForSize && (refNum != -1)) 
  97.     {
  98.     SizeRsrcHandle sizeHndl;
  99.     
  100.     gCheckForSize = false;//reset flag
  101.     gIntercept = false;//clear for next get1resource call
  102.  
  103.     //get size resource out of alias
  104.     sizeHndl = (SizeRsrcHandle)Get1Resource('SIZE', -1);
  105.     if (sizeHndl)
  106.         {
  107.         //found resource so load it and save info
  108.         LoadResource((Handle)sizeHndl);//load it cuz resload is false
  109.         
  110.         //save for later
  111.         gSizeResource = **sizeHndl;
  112.         gIntercept = true;
  113.         
  114.         //SetHFSDispatchFlag();
  115.         }
  116.     }
  117.     
  118. RestoreA4(oldA4);
  119.  
  120. return refNum;
  121. }
  122.  
  123. //get1resource patch from launch with size sample code from apple
  124. //tool chest may 97 cdrom
  125.  
  126. //Get1Resource patch will stuff a SIZE resource when a flag is set
  127. //in HOpenResFile, and its getting a SIZE resource
  128. static pascal Handle MyGet1ResourceC (ResType resType, short resID)
  129. {
  130. long oldA4 = SetUpA4();
  131.     
  132.     //call real one
  133.     Handle result = gOldGet1ResourceAddr(resType,resID);
  134.  
  135.     //wait for valid size resource comin back (might not be first
  136.     //one to try so the intercept flag is inside
  137.     if ( (resType == 'SIZE') && !ResError ( ) && result )
  138.         {
  139.         if (gIntercept)
  140.             {
  141.             gIntercept = false;//clear flag until nexttime
  142.         
  143.             //blast in my size resource
  144.             if (gSizeResource.preferred)
  145.                 **(SizeRsrcHandle)result = gSizeResource;
  146.             }
  147.         }
  148.  
  149.     RestoreA4 (oldA4);
  150.     
  151.     return result;
  152. }
  153.  
  154. static asm pascal Handle MyGet1ResourceAsm (ResType resType, short resID)
  155. {
  156.     move.l        a1,-(a7)            // save reg
  157.     move.l        d1,-(a7)            // save reg
  158.     move.l        d2,-(a7)            // save reg
  159.     subq.l        #4,a7                // room for result
  160.     move.l        22(a7),-(a7)        // push copy of resType
  161.     move.w        24(a7),-(a7)        // push copy of resID
  162.     jsr            MyGet1ResourceC        // do work
  163.     move.l        (a7)+,22(a7)        // copy and pop result
  164.     move.l        (a7)+,d2            // restore reg
  165.     move.l        (a7)+,d1            // restore reg
  166.     move.l        (a7)+,a1            // restore reg
  167.     move.l        (a7)+,a0            // ready return thru A0
  168.     addq.l        #6,a7                // pop args
  169.     jmp            (a0)                // phone home
  170. }
  171.